<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mpois extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
       
    }
    
    public function getPoisDB($number,$offset){   

	
        $limit = $offset ? intval($offset) : '0';
        $sql = "SELECT p.*, c.name AS cname FROM pois AS p INNER JOIN categories AS c ON p.category_id = c.id WHERE 1=1 ORDER BY p.id DESC  ";   
		$ss = " LIMIT ".$limit.",".$number." ";
		
		
        $count = $this->db->query($sql)->num_rows();
        $xs = $this->db->query($sql.$ss)->result_object();
		return array('data' => $xs, 'count' => $count);   
    }
    
	public function getCategories(){
		return $this->db->query('SELECT id, name FROM categories WHERE 1=1')->result_array();
	}
	
    public function addPoisDb($admin_id,$category_id,$name,$intro,$description,$link,$latitude,$longitude,$address,$phone,$favorite,$email,$active,$image){
        $data = array(
            'category_id' => $category_id,
			'name' => $name,
			'name_ascii' => $this->get_ascii($name),
			'intro' => $intro,
			'description' => $description,
			'link' => $link,
			'latitude' => $latitude,
			'longitude' => $longitude,
			'address' => $address,
			'phone' => $phone,
			'favorite' => $favorite,
			'email' => $email,
            'image' => $image,
            'active' => $active,
            'created' => date('Y-m-d H:i:s'),
			'admin_id' => $admin_id
        );
		//echo '<pre>';die(print_r($data));
        $this->db->insert('pois', $data);
        return 200;
    }
    
	public function deletePoisDB($id)
    {
        $this->db->query("DELETE FROM pois WHERE id = ".$id." ");
        return 200;
    }  
	
	public function getPoisDetail($id)
    {
        $data = $this->db->query("SELECT * FROM pois WHERE id = ".$id)->row_object();
        return $data;
    }
	
	
	
	
	
	public function editPoisDb($admin_id,$category_id,$name,$intro,$description,$link,$latitude,$longitude,$address,$phone,$favorite,$email,$active,$image,$id){
         $data = array(
            'category_id' => $category_id,
			'name' => $name,
			'name_ascii' => $this->get_ascii($name),
			'intro' => $intro,
			'description' => $description,
			'link' => $link,
			'latitude' => $latitude,
			'longitude' => $longitude,
			'address' => $address,
			'phone' => $phone,
			'favorite' => $favorite,
			'email' => $email,
            'image' => $image,
            'active' => $active,
			'admin_id' => $admin_id
        );
        $this->db->update('pois', $data,'id = '.$id.' ');
        return 200;
    }
    
}
